<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__stock}">
      <wms-form v-model="dataForm" :formList="formList" :table-column="tableColumn" :advanced="advanced"
        @check-change="checkChange" @changeAdvance="changeAdvance" @getDataList="getDataList" @getReset="getReset">
        <el-form-item>
          <el-button type="danger" plain @click="disables('disable')">一键禁用
          </el-button>
          <el-button type="success" plain @click="disables('enable')">一键解禁
          </el-button>
          <el-button type="primary" plain @click="exportHandle">库存导出
          </el-button>
          <el-button type="danger" @click="errorContAddHandle">异常空箱</el-button>
        </el-form-item>
      </wms-form>
      <wms-table ref="wmsTable" class="stockTable" v-model="dataForm" :page="page" :total="total" :limit="limit"
        :tableData="dataList" :table-column="column" :module-options="mixinViewModuleOptions"
        :dataListLoading="dataListLoading" :showSelection="true"
                 @selection-change="handleSelectionChange"
                 @pageSizeChangeHandle="pageSizeChangeHandle"
                 @pageCurrentChangeHandle="pageCurrentChangeHandle"
                 @errorContAddHandle="errorContAddHandle"
                 @changeLockSt="changeLockSt" />
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="errorContAddVisible" ref="errorContAdd" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./components/stock-add-or-update";
import debounce from "lodash/debounce";
import { formList, tableColumn } from './config'
import wmsTable from '@/components/wms-table'
import wmsForm from '@/components/wms-form'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/wms/stock/page",
        getDataListIsPage: true,
        exportURL: "/wms/stock/export",
        deleteURL: "/wms/stock",
        deleteIsBatch: true,
        deleteIsBatchKey: 'stockId'
      },
      dataForm: {
        spuNo: '',
        spuName: '',
        contNo: '',
        curPosition: ''
      },
      changeForm: {
        stockId: '',
        lockSt: ''
      },
      errorContAddVisible:false,
      selectedIds: [],
      formList,
      tableColumn,
      column: [],
      advanced: false
    };
  },
  components: {
    AddOrUpdate,
    wmsTable,
    wmsForm
  },
  methods: {
    //0.重置搜索框
    getReset() {
      this.dataForm = this.$options.data().dataForm;
    },
    // 处理表格多选
    handleSelectionChange(selection) {
      this.selectedIds = selection.map(item => item.stockId)
    },

    errorContAddHandle() {
      this.errorContAddVisible = true
      this.$nextTick(() => {
        this.$refs.errorContAdd.init()
        // this.$refs.errorContAdd.submitErrorForm();
      })
    },
    // 一键禁用操作
    batchHandle(statusType) {
      console.log(this.selectedIds)
      if (this.selectedIds.length === 0) {
        return this.$message.warning('请至少选择一条数据')
      }
      this.$confirm(`确定要${statusType ? '解禁' : '禁用'}选中库位吗？`, '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          const { data: res } = await this.$http.post('/wms/stock/disables', {
            ids: this.selectedIds,
            status: statusType
          })
          console.log(res)
          if (res.code !== 0) return this.$message.error(res.msg)
          this.$message.success(`${statusType ? '解禁' : '禁用'}成功`)
          this.getDataList() // 刷新数据
        } catch (err) {
          console.error('操作失败:', err)
        }
      })
    },
    // 修改按钮点击事件
    disables(type) {
      const statusType = type === 'enable' ? 1 : 0;  // 转换为状态值
      this.batchHandle(statusType);  // 传递参数
    },
    //1.库存冻结
    changeLockSt: debounce(function (val, row) {
      var tempStatus = row.lockSt === 0 ? 1 : 0;
      this.changeForm.stockId = row.stockId;
      this.changeForm.lockSt = row.lockSt;
      this.$http[!this.changeForm.stockId ? 'post' : 'put']('/wms/stock/', this.changeForm).then(({ data: res }) => {
        if (res.code !== 0) {
          //如果更新失败则重置为原来的状态
          row.lockSt = tempStatus;
          return this.$message.error(res.msg)
        }
        if (this.changeForm.lockSt === 1) {
          this.$message({
            message: this.$t('prompt.freezed'),
            type: 'success',
            duration: 500,
            onClose: () => {
            }
          })
        }
        if (this.changeForm.lockSt === 0) {
          this.$message({
            message: this.$t('prompt.thaw'),
            type: 'success',
            duration: 500,
            onClose: () => {
            }
          })
        }
      }).catch(() => { })
    }),
    freezeHandle(id) {
      if (!this.dataForm.stockId) {
        return this.$message({
          message: this.$t('prompt.freeze'),
          type: 'warning',
          duration: 500
        })
      }
      this.$confirm(this.$t('prompt.info', { 'handle': this.$t('freeze') }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post('/wms/stock/freeze/' + this.dataForm.stockId)
          .then(({ data: res }) => {
            if (res.code != 0) {
              return this.$message.error(res.msg)
            }
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 500,
              onClose: () => {
                this.getDataList()
              }
            })
          }).catch(() => { })


      }).catch(() => { })

    }
  }
}
</script>
<!--<style scoped lang="scss">-->
<!--/* 隐藏表头的全选框 */-->
<!--::v-deep .stockTable thead .el-table-column&#45;&#45;selection .cell {-->
<!--  display: none;-->
<!--}-->
<!--</style>-->
